Skip to content

interscript-py: reverse-mode semantics — dep-closure replay, gsub scan, replacement collapse (WO20 shrink-to-zero) - #869

Merged
ronaldtse merged 1 commit into
mainfrom
wo20-py-reverse-engine-fixes
Nov 18, 2026
Merged

ronaldtse merged 1 commit into
mainfrom
wo20-py-reverse-engine-fixes

Conversation

@ronaldtse

Copy link
Copy Markdown
Contributor

Summary

The py side of the WO20 reverse-golden shrink-to-zero (rs PR wo20-reverse-shrink-to-zero is the reference side; merge that first — it carries the grown golden this replay consumes). py was 123 of the 125 excluded maps; with this branch the full reverse replay is 0 deltas on 15,321 rows / 452 maps.

The headline fix is the dependency-closure replay: the compiled IR carries dependency names, so a reversed parent's run rules must execute reversed dependency maps (rs reverse_document recursion / ruby Dependency#reverse parity). The gate previously replayed reverse_map against forward-dep resolvers, so every run-carrying reversed doc executed the forward dependency stage — the 91-map identity-output family (alalc-rus-2012's "Azov" → "Azov" etc.). Runtime.reverse already had the right flow; the gate now mirrors it.

Plus five engine parity fixes: ruby-gsub scan semantics in parallel_megaregexp (python regex.sub continues at the same position after an empty match — bgnpcgn-prs/pus double-insert), repeat literal collapse, $N empty-insertion fallback, undefined-ref megaregexp drops, and line-anchored title_case.

test_executor's out-of-range capture-ref pin flips to the canonical empty-insertion semantics with the source-anchored justification (the old whole-match pin was the pre-fix ts behavior, diverging from rs and ruby).

Test plan

  • python3 -m pytest tests/: the only failure was test_golden_capture against the STALE forward golden — pre-existing on main (pristine rs main fails its own golden_capture identically; the rs refresh commit reconciles it). Green after the refresh.
  • test_golden_reverse.py + test_reverse.py green
  • Reverse golden replay vs rs wo20-reverse-shrink-to-zero: 15,321 rows, 0 delta maps
  • maps-CI additivity gates (py reverse step) on the merged state

@ronaldtse
ronaldtse force-pushed the wo20-py-reverse-engine-fixes branch from 62d92a5 to 5ec609d Compare November 18, 2026 14:24
…acement collapse - WO20 reverse shrink-to-zero

Six fixes closing the py side of the WO20 reverse-golden divergence
families - the largest, 123 of the 125 excluded maps (rs PR
wo20-reverse-shrink-to-zero is the reference side; with these the full
15,321-row reverse replay is 0-delta):

- test_golden_reverse replays through the DEPENDENCY-CLOSURE-reversing
  flow (Runtime.reverse parity): the compiled IR carries dependency
  NAMES, so a reversed parent's run rules must execute REVERSED
  dependency maps - rs reverse_document recurses into them and ruby
  reverses Dependency#document. Replaying reverse_map against
  forward-dep resolvers made every run-carrying reversed doc execute
  the FORWARD dependency stage - the 91-map identity-output family
  (alalc-rus-2012 "Azov" -> "Azov" etc.).
- parallel_megaregexp scans with ruby gsub semantics: python
  regex.sub continues at the SAME position after an empty match, so
  the next alternative ALSO fired there - double insertions where ruby
  inserts once (bgnpcgn-prs / bgnpcgn-pus). The scan now anchors each
  position (including end-of-string) and skips one character after a
  zero-width match, mirroring rs apply_parallel.
- Repeat items collapse to their inner literal in replacement position
  (ruby Repeat#nth_string parity) - reversed rules whose `to` carries
  a maybe restored nothing (bgnpcgn-ara-1956 / hin-Deva / odni-ara /
  yaghoubi families).
- `$N` group fallback inserts the EMPTY string, not the whole match -
  ruby gsub / rs decode_replacement semantics for unparticipated refs
  (the hin-Deva reversed `from "" to ref` rules).
- Megaregexp drops rules whose from-pattern backreferences a group the
  pattern does not define (rs: a ref with an unparticipated group
  never matches; python's combined compile raised "invalid group
  reference", bgnpcgn-zho).
- title_case upcases the first char of every LINE (ruby ^ is
  line-anchored), not just the string head
  (un-ell-Grek-Latn-1987-phonetic).

test_executor's out-of-range capture-ref pin flips to the canonical
empty-insertion semantics with the source-anchored justification (the
old whole-match pin was the ts pre-fix behavior, diverging from rs and
ruby).
@ronaldtse
ronaldtse force-pushed the wo20-py-reverse-engine-fixes branch from 5ec609d to 6601920 Compare November 18, 2026 14:25
@ronaldtse
ronaldtse merged commit 3e71297 into main Nov 18, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant